﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIS.ENT;
using SIS.BLL;

public partial class school_subjects : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadSchoolList();
            ddlSchoolList.DataBind();
            if (ddlSchoolList.Items.Count == 0)
            {
                lblMessage.Text = "Please set up a school profile firstly!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
                
            }

            if (ddlSchoolList.Items.Count > 0)
            {
                ddlSchoolList.Text = System.Configuration.ConfigurationManager.AppSettings["SchoolKeyDefault"].ToString();

                //ddlSchoolList.SelectedIndex = 0;
                //ddlSchoolList_SelectedIndexChanged(sender, e);
            }
        }
    }

    //private void LoadSchoolList()
    //{
    //    ddlSchoolList.Items.Clear();
    //    SchoolProfileTableList _profileList = SchoolProfileTableManager.GetList();
    //    if (_profileList != null)
    //    {
    //        foreach (SchoolProfileTable _profile in _profileList)
    //        {
    //            ddlSchoolList.Items.Add(new ListItem(_profile.SchoolName, _profile.SchoolKey.ToString()));
    //        }
    //    }
    //}

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlSchoolMajor.DataBind();
        ClearFields();
    }
    
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            SubjectTable _subject = new SubjectTable();
            _subject.SchoolKey = Convert.ToInt32(ddlSchoolList.Text);
            _subject.SchoolMajorKey = Convert.ToInt16(ddlSchoolMajor.Text);
            _subject.StandardLevelKey = Convert.ToInt32(ddlStandardLevel.Text);
            _subject.SubjectKey = Convert.ToInt32(lblSubjectKey.Text);
            _subject.SubjectCode = txtSubjectCode.Text;
            _subject.SubjectName = txtSubjectName.Text;
            _subject.Description = txtDescription.Text;
            _subject.Status = chkStatus.Checked;

            if (SubjectTableManager.Save(_subject) > 0)
            {
                ClearFields();
                grvSubject.DataBind();
                lblMessage.Text = "Saved sussccessfully!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblMessage.Text = "Saving failed!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (lblSubjectKey.Text.Length > 0)
        {
            if (SubjectTableManager.Delete(Convert.ToInt32(lblSubjectKey.Text)))
            {
                ClearFields();
                grvSubject.DataBind();
                lblMessage.Text = "Item was deleted!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblMessage.Text = "Deleting failed!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
        else
        {
            lblMessage.Text = "Please select an item to delete.";
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void grvSubject_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
        if (grvSubject.SelectedIndex >= 0)
        {
            DataKey _selectedDataKey = grvSubject.SelectedDataKey;
            int _subjectKey = Convert.ToInt32(_selectedDataKey[0]);
            //int _schoolKey = Convert.ToInt32(_selectedDataKey[1]);
            //int _schoolMajorKey = Convert.ToInt32(_selectedDataKey[2]);
            //int _standardLevelKey = Convert.ToInt32(_selectedDataKey[3]);

            if ( _subjectKey > 0)
            {
                //lblSchoolKey.Text = _schoolKey.ToString();
                //lblSchoolMajorKey.Text = _schoolMajorKey.ToString();
                //lblStandardLevelKey.Text = _standardLevelKey.ToString();
                lblSubjectKey.Text = _subjectKey.ToString();
                GridViewRow _selectedRow = grvSubject.SelectedRow;
                txtSubjectCode.Text = _selectedRow.Cells[4].Text;
                txtSubjectName.Text = _selectedRow.Cells[5].Text;
                txtDescription.Text = _selectedRow.Cells[6].Text;
                //lblMessage.Text = _selectedRow.Cells[4].Text;
                CheckBox _chkStatus = (CheckBox)(_selectedRow.Cells[7].Controls[0]);
                chkStatus.Checked = _chkStatus.Checked;
                //lblMessage.Text = _chkStatus.Checked.ToString();
            }
        }
    }

    private void ClearFields()
    {
        lblMessage.Text = "";
        lblSchoolKey.Text = ddlSchoolList.Text;
        lblSchoolMajorKey.Text = ddlSchoolMajor.Text;
        lblStandardLevelKey.Text = ddlStandardLevel.Text;
        lblSubjectKey.Text = "-1";
        txtSubjectCode.Text = "";
        txtSubjectName.Text = "";
        txtDescription.Text = "";
        chkStatus.Checked = false;
    }

    protected void ddlStandardLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        grvSubject.DataBind();
        ClearFields();
        
    }

    protected void btnShowSubject_Click(object sender, EventArgs e)
    {

    }

    protected void ddlSchoolMajor_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlStandardLevel.DataBind();
        ClearFields();
    }
}
